// pages/contact/contact.js
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var uid = 0;
Page({
  data:{
    userInfo:{},
    postuid: 0, //当前作者id
    uid: 0, //登录用户id
    token: '', //令牌
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    uid = options.uid;
    var inviteuid = options.inviteuid;
    this.setData({
      postuid: uid
    })
    //当前登录用户信息
    app.LoginUserInfo(function(res){
      //更新数据
      if (res && res != -1) {
      that.setData({
        userInfo: res,
        uid:res.uid,
        token: res.token
      })
      }
    }, inviteuid)
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  //发送私信
  sendMessage: function(e) {
    var privatekey = utilMd5.hexMD5(e.detail.value.uid+'postmessage'+e.detail.value.token);
    if(!e.detail.value.wx && !e.detail.value.qq && !e.detail.value.mobile) {
      app.dialog('请填写任意的联系方式', 'loading');
      return;
    }
    var msg = e.detail.value.message;
    if(!msg) {
      app.dialog('请填写留言内容', 'loading');
      return;
    }
    if(e.detail.value.wx) {
      msg += '\n微信号:'+e.detail.value.wx
    }
    if(e.detail.value.qq) {
      msg += '\nQQ:'+e.detail.value.qq
    }
    if(e.detail.value.mobile) {
      msg += '\n手机号:'+e.detail.value.mobile
    }
     var postdata = {
           publickey:util.publiccode,
            ac:"postmessage",
            uid:e.detail.value.uid,
            tuid:e.detail.value.postuid,
            message:msg,
            privatekey: privatekey
    }
    util.api({
        data:postdata,
        method:'POST',
        success:function(responseData){
            var data = responseData.data;
            if(data.msg === "true") {
              app.dialog('发送成功', 'success', function () {
                
              });
              wx.redirectTo({
                  url: '/pages/online-studio/online-studio?uid=' + postdata.tuid
              })         
            } else {
                app.dialog('发送失败', 'loading');
            }
        }
    });
  },
  goAtherPage: function(e){
      var url = e.currentTarget.dataset.link;
      wx.navigateTo({url: url})
  },
  //页面分享
  onShareAppMessage: function () {
    return app.sharePage('', uid);
  },
})